package de.bazinga.rotw.menu;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

import com.jme.image.Texture;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;
import com.jmex.game.state.GameStateManager;

import de.bazinga.rotw.game.Game;
import de.bazinga.rotw.game.RotwGameState;
import de.bazinga.rotw.menu.item.MenuItem;
import de.bazinga.rotw.menu.item.TextItem;

public class OptionsMenu extends Menu {
	
	public OptionsMenu() {
		super("optionsMenu");
	}
	
	protected void init() {		
		// Background
		Quad bgQuad = new Quad("bg",
				Game.getGame().getDisplay().getWidth(), 
				Game.getGame().getDisplay().getHeight());
		
		bgQuad.setRenderQueueMode(Renderer.QUEUE_ORTHO);   
	 	
		bgQuad.setLocalTranslation(new Vector3f(
				Game.getGame().getDisplay().getWidth()/2,
				Game.getGame().getDisplay().getHeight()/2,
				0));
		
		TextureState bg = Game.getGame().getDisplay().getRenderer().createTextureState();
		bg.setEnabled(true);
		bg.setTexture(TextureManager.loadTexture(RotwGameState.class
				.getClassLoader().getResource("background/menuBg.png"),
				Texture.MinificationFilter.BilinearNearestMipMap,
				Texture.MagnificationFilter.Bilinear));
		
		bgQuad.setRenderState(bg);
		
		bgQuad.setLightCombineMode(Spatial.LightCombineMode.Off);
		bgQuad.updateRenderState();
		
		getRootNode().attachChild(bgQuad);
	}
	
	protected void initMenuItems() {
		otherItems.put("soundTitle",
				new MenuItem("sound.png",
						"soundTitle",
						2f,
						150,
						Game.getGame().getDisplay().getHeight() - 150,
						250,100));
		
		String icon = (Game.getGame().getSettings().isSFX()) ? "check/check.png":"check/cross.png";
		
		menuItems.put("sound", 
				new MenuItem(icon,
						"sound",
						2f,
						350,
						Game.getGame().getDisplay().getHeight() - 150,
						64,64));
		
		
		menuItems.put("usernameButton",
				new MenuItem("options/username.png",
						"usernameButton",
						2f,
						150,
						Game.getGame().getDisplay().getHeight()-250,
						250,100));
		
		
		textItems.put("username", 
				new TextItem("",
						"username", 
						ColorRGBA.yellow,
						ColorRGBA.black,
						2f, 
						270, 
						Game.getGame().getDisplay().getHeight() - 260));
		String currName = ((RotwGameState)GameStateManager.getInstance().getChild("gameState")).getProps().getProperty("playerName");
		textItems.get("username").setText(textItems.get("username").getText() + currName);
		

		
		menuItems.put("gameCodeButton",
				new MenuItem("options/gameCode.png",
						"gameCodeButton",
						2f,
						150,
						Game.getGame().getDisplay().getHeight()-350,
						250,100));
		
		
		
		textItems.put("gameCode", 
				new TextItem("",
						"gameCode", 
						ColorRGBA.yellow,
						ColorRGBA.black,
						2f, 
						270, 
						Game.getGame().getDisplay().getHeight() - 360));
		String gameCode = ((RotwGameState)GameStateManager.getInstance().getChild("gameState")).getProps().getProperty("gameCode");
		textItems.get("gameCode").setText(textItems.get("gameCode").getText() + gameCode);

		menuItems.put("save", 
				new MenuItem("options/save.png",
						"save",
						2f,
						150,
						Game.getGame().getDisplay().getHeight() - 450,
						250,100));
		
		menuItems.put("back",
				new MenuItem("back.png",
						"back",
						2f,
						150,
						Game.getGame().getDisplay().getHeight()-550,
						250,100));
	}
	
	protected void handleClick(String item) {
		if(menuItems.containsKey(item)) {
			if(item.equals("sound")) {
				
				Game.getGame().getSettings().setSFX(!Game.getGame().getSettings().isSFX());
				if(Game.getGame().getSettings().isSFX()) {
					menuItems.get("sound").setText("check/check.png");
					Game.startMenuSound();
				}else{
					menuItems.get("sound").setText("check/cross.png");
					Game.stopMenuSound();
				}
				
			}else if(item.equals("save")) {
				// Save Username
				((RotwGameState)GameStateManager.getInstance().getChild("gameState")).getProps()
					.setProperty("playerName", textItems.get("username").getTextOutput());
				((RotwGameState)GameStateManager.getInstance().getChild("gameState")).getProps()
					.setProperty("gameCode", textItems.get("gameCode").getTextOutput());
				
				try {
					File propFile = new File("gameProps");
					// Create file if it doesnt exist yet
					if(!propFile.exists()) propFile.createNewFile();
					// Save Properties
					FileOutputStream os = new FileOutputStream(propFile);
					((RotwGameState)GameStateManager.getInstance().getChild("gameState")).getProps().store(os, "newSettings");
					os.close();
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				// Show Info-Message
				/*((InfoWindow)GameStateManager.getInstance().getChild("infoWindow")).clearInfoText();
				((InfoWindow)GameStateManager.getInstance().getChild("infoWindow")).addInfoText("Saved");
				((InfoWindow)GameStateManager.getInstance().getChild("infoWindow")).addInfoText("Your settings have been saved!");
				*/
				this.setActive(false);
				GameStateManager.getInstance().getChild("mainMenu").setActive(true);
			}else if(item.equals("back")) {
				this.setActive(false);
				GameStateManager.getInstance().getChild("mainMenu").setActive(true);
			}else if(item.equals("usernameButton")) {
				textItems.get("username").setActive(true);
			}else if(item.equals("gameCodeButton")) {
				textItems.get("gameCode").setActive(true);
			}
		}
	}
	
	protected void handleHover(MenuItem hoverItem) {
		Iterator<MenuItem> it = menuItems.values().iterator();
		while(it.hasNext()) {
			MenuItem item = it.next();
			if(hoverItem == item) {
				if(item == menuItems.get("back")) {
					item.setText("backHover.png");
				}else if(item == menuItems.get("save")) {
					item.setText("options/saveHover.png");
				}else if(item == menuItems.get("usernameButton")) {
					item.setText("options/usernameHover.png");
				}else if(item == menuItems.get("gameCodeButton")) {
					item.setText("options/gameCodeHover.png");
				}else{
					item.setTextColor(ColorRGBA.yellow);
				}
			}else{
				if(item == menuItems.get("back")) {
					item.setText("back.png");
				}else if(item == menuItems.get("save")) {
					item.setText("options/save.png");
				}else if(item == menuItems.get("usernameButton")) {
					item.setText("options/username.png");
				}else if(item == menuItems.get("gameCodeButton")) {
					item.setText("options/gameCode.png");
				}else{
					item.setTextColor(ColorRGBA.black);
				}
			}
		}
	}
}
